;layui.define(['jquery', 'sha'], function (exports) {
    "use strict";
    let $ = layui.$,
        sha = layui.sha,
        MOD_NAME = "fa_tab",
        debug = true,
        log = function log(...msg) {
            debug && console.log(MOD_NAME, ...msg)
        };
    let Fa_Tab = function () {
        this.options = {
            tab: $('.fa-tab'),
            con: $('.fa-content'),
            pan: $('.fa-tab-pan ul'),
            itemd_cls: 'fa-selected',
            con_item_cls: '.fa-content-item',
            item_cls: '.fa-tab-item',
            id_key: 'fa-tab-id',
            prev: $('.fa-tab-prev'),
            next: $('.fa-tab-next'),
            ctr: $('.fa-tab-ctr'),
            ctr_more: $('.fa-tab-ctr-more'),
            lock_cls: 'fa-tab-locked',
            minLeft: 0,
        }
        this.init({a: 1});
    }
    Fa_Tab.prototype.init = function (options) {
        let _this = this;
        $.extend(_this.options, options);
        this.resize();
        this.initToolBar();

    }
    Fa_Tab.prototype.initToolBar = function () {
        let _this = this, w = this.options.pan.innerWidth() / 2;
        this.options.prev.on('click', function (e) {
            _this.scroll(w);
        })
        this.options.next.on('click', function (e) {
            _this.scroll(-1 * w);
        })
        this.options.ctr.on('click', function (e) {
            _this.options.ctr_more.toggleClass('fa-tab-ctr-more-on')
        })
        this.options.ctr_more.children('li').on('click', function (e) {
            let tt = $(e.target).attr('fa-event');
            _this.ctr(tt);
        })
    }
    Fa_Tab.prototype.scroll = function (len) {
        let pan = this.options.pan;
        len = len + (parseInt(pan.css('left') || 0));
        len = len > 0 ? 0 : len;
        len = len < this.options.minLeft ? this.options.minLeft : len;
        console.log(len)
        pan.css('left', len);
    }
    Fa_Tab.prototype.ctr = function (tt) {
        let _this = this;
        switch (tt) {
            case 'refresh':
                let ss = $(_this.cons[_this.idx].children[0]);
                ss.attr('src', ss.attr('src'));
                break;
            case 'closeOth':
                _this.tabs.each(function (idx, item) {
                    if (_this.idx != idx && !$(item).hasClass(_this.options.lock_cls)) {
                        $(item).remove();
                        $(_this.cons[idx]).remove();
                    }
                });
                break;
            case 'closeAll':
                _this.tabs.each(function (idx, item) {
                    if (!$(item).hasClass(_this.options.lock_cls)) {
                        $(item).remove();
                        $(_this.cons[idx]).remove();
                    } else {
                        _this.show(item);
                    }
                });
                break;
        }
    }
    Fa_Tab.prototype.show = function (ele) {
        let idx = parseInt($(ele).attr(this.options.id_key));
        if (this.idx == idx) {
            return;
        }
        $(this.tabs[this.idx]).removeClass(this.options.itemd_cls);
        $(this.cons[this.idx]).hide();
        this.idx = idx;
        $(this.tabs[this.idx]).addClass(this.options.itemd_cls);
        $(this.cons[this.idx]).show();
    }
    Fa_Tab.prototype.buildCon = function (content) {
        this.options.con.append(['<div class="fa-content-item ">', content, '</div>'].join(''))
    }
    Fa_Tab.prototype.buildTab = function (uid, title) {
        let r = ['<li class="fa-tab-item" uid="', uid, '"', this.options.id_key, '=', this.cons.size(), '><span>', title, '</span><i class="layui-icon layui-icon-close"></i></li>'].join('')
        this.options.pan.append(r);
    }
    Fa_Tab.prototype.close = function (ele) {
        if ($(ele).hasClass(this.options.lock_cls)) {
            return;
        }
        let idx = parseInt($(ele).attr(this.options.id_key));
        if (idx == this.idx) {
            let n = this.tabs[(idx + 1) >= this.tabs.size() ? (idx - 1) : (idx + 1)];
            this.show(n);
        }
        $(this.tabs[idx]).remove();
        $(this.cons[idx]).remove();
        this.resize();
    }
    Fa_Tab.prototype.open = function (title, url, target) {
        log(title, url, target);
        let _this = this, hasOpened = false, uid = sha.hex_sha1(url);
        this.tabs.each(function (idx, item) {
            log(uid, $(item).attr('uid'));
            if (uid == $(item).attr('uid')) {
                _this.show(item);
                hasOpened = true;
                return;
            }
        });
        if (hasOpened) {
            return;
        }
        switch (target) {
            case 'TAB':
                this.buildTab(uid, title)
                this.buildCon(['<iframe src="', url, '"></iframe>'].join(''));
                this.resize();
                this.show(this.tabs[this.tabs.size() - 1]);
                break;
        }
    }
    Fa_Tab.prototype.resize = function () {
        let _this = this,
            o = this.options,
            tab = o.tab,
            con = o.con;
        this.tabs = tab.find(o.item_cls);
        this.cons = con.find(o.con_item_cls);
        _this.options.minLeft = 0;
        this.tabs.each(function (idx, item) {
            $(item).attr(o.id_key, idx);
            _this.options.minLeft += $(item).outerWidth();
        });
        this.show(this.options.tab.find("." + this.options.itemd_cls)[0]);
        _this.options.minLeft = this.options.pan.innerWidth() - _this.options.minLeft;
        _this.options.minLeft = (_this.options.minLeft > 0) ? 0 : (_this.options.minLeft - this.options.pan.innerWidth() / 4);
        this.tabs.unbind('click');
        this.tabs.find('.layui-icon-close').unbind('click');
        this.tabs.on('click', function (event) {
            _this.show(this);
            event.stopPropagation();
            return false;
        })
        this.tabs.find('.layui-icon-close').on('click', function (event) {
            _this.close($(this).parent(_this.options.item_cls));
            event.stopPropagation();
            return false;
        })
    }
    exports(MOD_NAME, {
        tab: new Fa_Tab(),
        open: function (title, url, target) {
            this.tab.open(title, url, target);
        }
    });
});